package block;

import java.awt.*;

public class SimpleLogicBlock extends LogicBlock{

    private int type; // OR: -1 AND: 1 NOT: 0

    public SimpleLogicBlock(int[] inputs, boolean[][] taken){
        super(inputs[0], inputs[1], inputs[2], inputs[3], taken);
        this.type = inputs[4];
    }

    @Override
    public void run(int[][] cond){
        super.run(cond);
        if(outputs == -1)return;
        switch(type){
            case -1:
                outputs = 0;
                for(int i = startY; i < startY + row; i ++)
                    if(cond[startX][i] == 1){
                        outputs = 1;
                        break;
                    }
                break;
            case 1:
                outputs = 1;
                for(int i = startY; i < startY + row; i ++)
                    if(cond[startX][i] == 0) {
                        outputs = 0;
                        break;
                    }
                break;
            case 0:
                outputs = cond[startX][startY] == 1 ? 0 : 1;
                break;
        }
        for(int j = startY; j < startY + row; j ++)
            cond[startX + column][j] = outputs;
    }

    @Override
    public void draw(Graphics g, int unit, int zeroX, int zeroY){
        super.draw(g, unit, zeroX, zeroY);
        switch(type){
            case -1:
                g.drawString("OR:" + (outputs == -1 ? "" : outputs), zeroX + unit * startX, zeroY + unit * startY + 12);
                return;
            case 0:
                g.drawString("NOT:" + (outputs == -1 ? "" : outputs), zeroX + unit * startX, zeroY + unit * startY + 12);
                return;
            case 1:
                g.drawString("AND:" + (outputs == -1 ? "" : outputs), zeroX + unit * startX, zeroY + unit * startY + 12);
                return;
        }
    }

    @Override
    public String toString(){
        return "startX:" + startX + " startY:" + startY + " column:" + column + " row:" + row + "type:" + type + "\n";
    }

    public void fulfill(int[] inputs){
        if(inputs[0] == -2)
            inputs[0] = startX;
        if(inputs[1] == -2)
            inputs[1] = startY;
        if(inputs[2] == -2)
            inputs[2] = column;
        if(inputs[3] == -2)
            inputs[3] = row;
        if(inputs[4] == -2)
            inputs[4] = type;
    }
}
